<?php


namespace Xy\Rbac\Models;



use Xy\Rbac\ModelTools\BaseModel;

class RbacPowerPageModel extends BaseModel
{
    protected $dateFormat = 'Y-m-d H:i:s';
    
    protected $appends = [
        'meta',
        'name',
    ];
    
    protected $hidden = [
        'deleted',
    ];
    
    /**
     * level 目录等级字段
     */
    const LEVEL_DIR  = 1;//导航目录
    const LEVEL_PAGE = 2;//导航页面
    const LEVEL_API  = 3;//页面api
    
    /**
     * status 状态字段
     */
    const STATUS_YES       = 1;
    const STATUS_YES_TITLE = '启用';
    const STATUS_NO        = 2;
    const STATUS_NO_TITLE  = '未启用';
    
    public function __construct(array $attributes = [])
    {
        array_push($this->appends, 'label');
        parent::__construct($attributes);
    }
    
    public function getLabelAttribute()
    {
        return $this->title;
    }
    
    /**
     * meta信息组织
     * @return array
     */
    public function getMetaAttribute() {
        return [
            "hideInBread"     => !!$this->meta_hide_in_bread,
            "hideInMenu"      => !!$this->meta_hide_in_menu,
            "notCache"        => !!$this->meta_not_cache,
            "access"          => $this->meta_access,
            "icon"            => $this->meta_icon,
            "beforeCloseName" => $this->meta_before_close_name,
            "pinyin"          => $this->meta_pinyin,
            "title"           => $this->title,
            "showAlways"      => !!$this->meta_show_always,
        ];
    }
    
    /**
     * vue路由name属性获取
     * @return mixed
     */
    public function getNameAttribute() {
        return $this->route_name;
    }
    
    /**
     * @param $val
     */
    public function setPropsAttribute($val) {
        $propsJson = json_encode((array)$val);
        $propsJson = strlen($propsJson) < 780 ? $propsJson : "{}";//长度大于780不进行存储，先这样粗略处理
        
        $this->attributes['props'] = $propsJson;
    }
    
    /**
     * @param $val
     *
     * @return array
     */
    public function getPropsAttribute($val) {
        $props = json_decode($val, true);
        
        return $props && is_array($props) ? $props : null;
    }
}